<?php
require_once('vobject/ImageVO.php');
require_once('connexion/Mysql.php');
require_once('connexion/SQLRequest.php');
/**
 * Class FlashGallery
 */
class FlashGallery
{
	private $db;
	
    function FlashGallery()
	{
		$this->methodTable["getAllImages"]['access'] = 'remote';
		$this->methodTable["getAllImages"]['description'] = 'Return the whole images list';
		
		$this->methodTable["getImagesByRange"]['access'] = 'remote';
		$this->methodTable["getImagesByRange"]['description'] = 'Return the images list, from a specified range';
		$this->methodTable["getImagesByRange"]['arguments'] = 'startIndex';				
		
		$this->db = Mysql::getInstance( '127.0.0.1', 'flash_gallery', 'root', '' );	
	}
	/**
	  * getAllImages
	  * @returns all images from the database
	  */
	 public function getAllImages() 
	 {		
		$this->db->Open();
		
		$request = SQLRequest::getRequest( "getAllImages" );
		$result = $this->db->Query( $request );
		
		$rest = self::fetchDataIntoArray( $result );
        
		$this->db->Close();

        // on retourne le tableau
        return $rest;
	 }
	 
	 /**
	  * getImagesByRange
	  * @returns some images from the database, based on a starting index
	  * @param startIndex the starting index
	  */
	 public function getImagesByRange( $startIndex = 0 ) 
	 {		
		$this->db->Open();
		
		$request = SQLRequest::getRequest( "getImagesByRange", $startIndex );
		$result = $this->db->Query( $request );
		
		$rest = self::fetchDataIntoArray( $result );
        
		$this->db->Close();

        // on retourne le tableau
        return $rest;
	 }
	 
	 static private function fetchDataIntoArray( $result )
	 {
	 	$rest = array();
		
	 	while( $data = mysql_fetch_assoc( $result ) )
        {
            $id 			= $data['image_id'];
            $name 			= $data['image_name'];
            $description	= $data['image_description'];
			$width 			= $data['image_width'];
			$height 		= $data['image_height'];
	    	
			$values = array(
							"id"=>$id,
							"name"=>$name,
							"description"=>$description,
							"width"=>$width,
							"height"=>$height
							);
			
	    	$image = new ImageVO( $values );
            array_push($rest, $image);
        }
		
		return $rest;
	 }
}
?>